/**
 * 데이터 송수신 
 * 조건분기 처리
 */

package network;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;

import javax.swing.DefaultListModel;

public class MultiChatServerThread extends Thread {
	MultiChatServer server;
	Socket socket;
	boolean tFlag=true;
	ObjectOutputStream send;
	ObjectInputStream receive;
	DefaultListModel<String> model;

	public MultiChatServerThread(MultiChatServer m, Socket c) {
		this.server = m;
		this.socket = c;
	}

	public void run() {
		try {
			send = new ObjectOutputStream(socket.getOutputStream());
			receive = new ObjectInputStream(socket.getInputStream());
			
			
			while (tFlag) {
				SocketData sd = (SocketData) receive.readObject();

				
				if ("login".equals(sd.getCommand())) {
					server.clients.put(sd.getUser(), this);

//					Collection<String> c = server.clients.keySet();
//					Object temp[] = c.toArray();
//					server.list.setListData(temp);
//					
					sd.setCommand("userlist");
//					sendUserList(sd,c);
					
					server.dataSet(sd);
					listSet(sd);
					
				} else if ("message".equals(sd.getCommand())) {
					server.dataSet(sd);
					sendMsg(sd);//내용을 모든 클라이언트에 뿌려줌
					
				} else if ("logout".equals(sd.getCommand())) {
					server.dataSet(sd);
					server.clients.remove(sd.getUser());
					listSet(sd);
					
//					tFlag = false;
//					server.dataSet(sd);
//					sendMsg(sd);
//					server.clients.remove(sd.getUser());
//					Collection<String> c = server.clients.keySet();
//					Object temp[] = c.toArray();
//					server.list.setListData(temp);
					
				}  
				
				else if (sd.getCommand().equals("fileTrans")) {
				FileServer fs = new FileServer(null);
				fs.setVisible(true);
//					server.clients.remove(sd.getUser());
//					server.clients.put(sd.getMessage(),this);
//					server.dataSet(sd.getUser()+"님의 닉네임이 "+ sd.getMessage()+"로 변경되었습니다.");
//				}else if{
				}			
				
				}
		} catch (Exception e) {
			
			
			InetAddress ia = socket.getInetAddress();
			String rIP=ia.getHostAddress();
			System.out.println("오류클라이언트 : "+rIP);
			
			
			e.printStackTrace();
		}
	}

	public void sendMsg(SocketData sd) {
		try {
			Set<String> st = server.clients.keySet();
			Iterator<String> it = st.iterator();
			while (it.hasNext()) {
				String key = it.next();
				server.clients.get(key).send.writeObject(sd);
			}
			
//			Collection<String> = server.clients.keySet();
//			Iterator<String> it = c.iterator();
//			while(it.hasNext()){
//				MultiChatServerThread t = server.clients.get(it.next());
//				t.send.writeObject(sd);
//			}
			
		} catch (Exception e) {
		
			e.printStackTrace();
		}
	}
	
	public void listSet(SocketData sd){ 
		server.model.clear();
		Set<String> st = server.clients.keySet();
		Iterator<String> it = st.iterator();
		while(it.hasNext()){
			String key = it.next();
			server.model.addElement(key);
		}
		
		try {
			st = server.clients.keySet();
			it = st.iterator();
			sd.setCommand("userlist");
			sd.setObj(server.model);

			sendMsg(sd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	//클라이언트가 접속되면 서버가 저장되고 있는 모든 유저 목록을 object에 담아 모든 유저
//	public void sendUserList(SocketData sd, Collection<String> c){
//		Iterator<String> it = c.iterator();
//		Vector v = new Vector<>();
//		
//		while(it.hasNext()){
//			v.add(it.next());
//		}
//		
//		sd.setObj(v);
//		
//		sendMsg(sd);
//	}
	

}
